package com.hclc.controller;

import com.hclc.entity.model.contans.ApiConst;
import com.hclc.entity.model.result.ResultEntity;
import com.hclc.util.password.BCryptPasswordEncoder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName demoCommonsIDGenertator
 * @Author: yurj
 * @Mail：1638234804@qq.com
 * @Date: Create in 15:52 2020/8/23
 * @version: 1.0
 */
@Api(tags = "1.0 - aa-Generate", description = "公用生成器")
@RestController
@RequestMapping("console/ids")
@Validated
public class DemoCommonsIDGenerate {
    /**
     * 获取随机主键
     *
     * @param
     * @return com.hclc.yxb.model.result.ResultEntity
     * @author yurj
     * @mail 1638234804@qq.com
     * @version 1.0
     * @date 2020/8/23 15:58
     */
    @ApiOperation("获取随机主键")
    @PostMapping(value = "/getRandomPrimaryKey", produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResultEntity getRandomPrimaryKey() {
        List<String> ids = new ArrayList<>();
        for (int i = 0; i < ApiConst.DEFAULT_INITIAL_CAPACITY; i++) {
            long mostSignificantBits = UUID.randomUUID().getMostSignificantBits();
            if (mostSignificantBits > 0) {
                ids.add(String.valueOf(mostSignificantBits));
            }
        }
        return ResultEntity.ResultSuccess(ids);
    }

    /**
     * 获取随机密码
     *
     * @param
     * @return com.hclc.yxb.model.result.ResultEntity
     * @author yurj
     * @mail 1638234804@qq.com
     * @version 1.0
     * @date 2020/8/23 15:58
     */
    @ApiOperation("获取随机密码")
    @PostMapping(value = "/getCredentialByPasswordEncoder", produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResultEntity getCredentialByPasswordEncoder(@ApiParam(value = "加密原字符", defaultValue = "e10adc3949ba59abbe56e057f20f883e") @RequestParam String value) {
        // 加密策略
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return ResultEntity.ResultSuccess(passwordEncoder.encode(value));
    }
}
